package com.wh.shiroswagger.config;

import com.wh.shiroswagger.filter.CustomFilter;
import com.wh.shiroswagger.realm.CustomCredentialMatcher;
import com.wh.shiroswagger.realm.CustomRealm;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Auther: HuiEr
 * @Date: 2020/3/5 22:37
 * @Description
 */
@SpringBootConfiguration
public class ShiroConfig {
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(){
        /*ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager());
        LinkedHashMap<String, Filter> stringFilterLinkedHashMap = new LinkedHashMap<>();
        stringFilterLinkedHashMap.put("token",customFilter());
        shiroFilterFactoryBean.setFilters(stringFilterLinkedHashMap);
        LinkedHashMap<String, String> maps1 = new LinkedHashMap<>();
        maps1.put("/login","anon");

        maps1.put("/swagger/**","anon");
        maps1.put("/v2/api-docs","anon");
        maps1.put("/swagger-ui.html","anon");
        maps1.put("/swagger-resources/**","anon");
        maps1.put("/webjars/**","anon");
        maps1.put("/favicon.ico","anon");
        maps1.put("/captcha.jpg","anon");
        maps1.put("/csrf","anon");

        maps1.put("/**","token,authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(maps1);
        return shiroFilterFactoryBean;*/

        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //配置安全管理器
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager());
        //接下来配置些过滤器
        //配置自己的那个过滤器

        Map<String, Filter> maps=new LinkedHashMap<>();
        maps.put("token",new CustomFilter());
        shiroFilterFactoryBean.setFilters(maps);
        //对请求过滤和拦截的设置
        Map<String,String> maps1=new LinkedHashMap<>();
        //放入不拦截的页面  拦截的页面....
        maps1.put("/login","anon");
        //Swagger的所有请求的资源和请求的地址都不需要拦截
        maps1.put("/swagger/**","anon");
        maps1.put("/v2/api-docs","anon");
        maps1.put("/swagger-ui.html","anon");
        maps1.put("/swagger-resources/**","anon");
        maps1.put("/webjars/**","anon");
        maps1.put("/favicon.ico","anon");
        maps1.put("/captcha.jpg","anon");
        maps1.put("/csrf","anon");
        //设置我们自己的校验


        maps1.put("/**","token,authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(maps1);
        return shiroFilterFactoryBean;
    }

    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(customRealm());
        return defaultWebSecurityManager;
    }

    @Bean
    public CustomRealm customRealm(){
        CustomRealm customRealm = new CustomRealm();
        customRealm.setCredentialsMatcher(customCredentialMatcher());
        return customRealm;
    }

    @Bean
    public CustomCredentialMatcher customCredentialMatcher(){
        CustomCredentialMatcher customCredentialMatcher = new CustomCredentialMatcher();
        return customCredentialMatcher;
    }

    /*@Bean
    public CustomFilter customFilter(){
        CustomFilter customFilter = new CustomFilter();
        return customFilter;
    }*/

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultSecurityManager securityManager){
        AuthorizationAttributeSourceAdvisor attributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        attributeSourceAdvisor.setSecurityManager(securityManager);
        return attributeSourceAdvisor;
    }
    @Bean
    @ConditionalOnMissingBean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
        return defaultAdvisorAutoProxyCreator;
    }
}
